/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package manager;

import JDBCUtil.MyConn;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import model.admin.Religion;

/**
 *
 * @author Danh
 */
public class ReligionManager {
     Connection conn;

    public ReligionManager() {
        try {
            conn = MyConn.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<Religion> getReligon() {
        List<Religion> list = new ArrayList<Religion>();
        try {
            PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM Religion_Types");
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Religion c = new Religion(rs.getInt("Religion_Type_Id"), rs.getNString("Religion_Type_Name"), rs.getBoolean("Religion_Type_Status"));
                list.add(c);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean insertReligon(Religion r) {
        try {
            PreparedStatement pstmt = conn.prepareStatement("INSERT INTO Religion_Types(Religion_Type_Name,Religion_Type_Status) VALUES(?,?)");
            pstmt.setNString(1, r.getReligionName());
            pstmt.setBoolean(2, r.isReligionStatus());
            int eu = pstmt.executeUpdate();
            if (eu > 0) {
                return true;
            }
        } catch (Exception e) {
        }
        return false;
    }

    public boolean update(Religion c) {
        PreparedStatement pstmt;
        try {
            pstmt = conn.prepareStatement("UPDATE Religion_Types SET Religion_Type_Status=? ,Religion_Type_Name =? WHERE Religion_Type_Id=?");
            pstmt.setBoolean(1, c.isReligionStatus());
            pstmt.setNString(2, c.getReligionName());
            pstmt.setInt(3, c.getReligionId());
            int eu = pstmt.executeUpdate();
            if (eu > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean deleteReligion(int id) {
        try {
            PreparedStatement pstmt = conn.prepareStatement("DELETE FROM Religion_Types  WHERE Religion_Type_Id=?");
            pstmt.setInt(1, id);
            int eu = pstmt.executeUpdate();
            if (eu > 0) {
                return true;
            }
        } catch (Exception e) {
        }

        return false;
    }
}
